import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';
import 'package:getx_app/app/routes/app_pages.dart';
import 'package:getx_app/app/widgets/click_item.dart';

import 'logic.dart';

class ProfilePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final logic = Get.put(ProfileLogic());
    final state = Get
        .find<ProfileLogic>()
        .state;

    Widget _buildAvatarRow() {
      if (state.userInfo == null) {
        return SizedBox.shrink();
      }

      return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Get.toNamed(Routes.PROFILE_DETAIL);
        },
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
          child: Row(
            children: [
              Expanded(
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      SizedBox.fromSize(
                        size: Size.square(80),
                        child: CircleAvatar(
                          backgroundImage: (state.userInfo!.avatar != null
                              ? NetworkImage(state.userInfo!.avatar)
                              : AssetImage('assets/images/avatar.png') as ImageProvider),
                        ),
                      ),
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 10),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                state.userInfo!.nickname!,
                                style: TextStyle(fontSize: 20, color: Color(0xff2e3944)),
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: 6),
                                child: Text(
                                  "查看并编辑个人资料",
                                  style: TextStyle(color: Color(0xff9d9fa7)),
                                ),
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  )),
              Icon(
                Icons.arrow_forward_ios_outlined,
                size: 16,
                color: Color(0xff969799),
              ),
            ],
          ),
        ),
      );
    }

    Widget _buildTabRow() {
      final cardWidth = MediaQuery
          .of(context)
          .size
          .width / 2 - 30;
      final _cardSize = Size(cardWidth, 80);
      final _iconColor = Theme
          .of(context)
          .colorScheme
          .secondary;
      return GestureDetector(
        onTap: () {},
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Card(
                child: Ink(
                  child: InkWell(
                    onTap: () {},
                    child: SizedBox.fromSize(
                      size: _cardSize,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("我的订阅"),
                          SizedBox(
                            width: 20,
                          ),
                          Icon(
                            Icons.bookmark,
                            color: _iconColor,
                          )
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              Card(
                child: Ink(
                  child: InkWell(
                      onTap: () {},
                      child: SizedBox.fromSize(
                        size: _cardSize,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text("我的消息"),
                            SizedBox(
                              width: 20,
                            ),
                            Icon(
                              Icons.sms,
                              color: _iconColor,
                            )
                          ],
                        ),
                      )),
                ),
              ),
            ],
          ),
        ),
      );
    }

    Widget _buildLeading(String title, IconData iconData) {
      return Row(
        children: [
          Icon(
            iconData,
            color: Color(0xff646464),
            size: 18,
          ),
          SizedBox(
            width: 10,
          ),
          Text(title)
        ],
      );
    }

    Widget _buildMenuList() {
      return Card(
        shadowColor: Color(0x80DCE7FA),
        child: Column(
          children: [
            ClickItem(
              leading: _buildLeading("我的订单", Icons.wysiwyg),
              onTap: () {},
            ),
            ClickItem(
              leading: _buildLeading("内容分类", Icons.poll),
              onTap: () {},
            ),
            ClickItem(
              bordered: false,
              leading: _buildLeading("设置", Icons.settings),
              onTap: () {},
            )
          ],
        ),
      );
    }

    return GetBuilder<ProfileLogic>(builder: (logic) {
      return Scaffold(
        body: SafeArea(
          top: true,
          child: state.isLoading
              ? Center(
            child: SpinKitPulse(
              color: Theme
                  .of(context)
                  .colorScheme
                  .secondary,
              size: 50.0,
            ),
          )
              : SingleChildScrollView(
            child: Column(
              children: [
                _buildAvatarRow(),
                _buildTabRow(),
                SizedBox(
                  height: 20,
                ),
                _buildMenuList(),
              ],
            ),
          ),
        ),
      );
    });
  }
}
